// 管理mock数据，专业管理,要导入到mock的index
import * as mUtils from '@/util/mUtils'

let majorRecord=[]; //存储专业记录
const num=100      //模拟的专业信息数据总数

for(let i=1;i<=num;i++){
    majorRecord.push({
        majorId:i.toString(),               //专业ID,后台用来区分，前台不用不显示
        name:'计算机专业'+i.toString(),       //专业名称
        collegeName:"计算机学院"+i.toString(),//学院名称
    });
}

export default {
    //专业信息列表
    getMajorList: config => {
        const { currentPage, rows } = mUtils.param2Obj(config.url);
        // console.log("获取所有信息的token：",token);
        // console.log("获取专业所有信息，学院ID：",collegeId,currentPage,rows)
        const list = majorRecord.filter((item,index) => index < rows * currentPage && index >= rows * (currentPage - 1))
        return {
            data:{
                majorRecord:list,
                count:majorRecord.length,
            },
            code:1,
            msg:'获取成功'
        }
    },
    //表格中删除特定专业
    deleteMajor:config=>{
        const { token,majorId} = mUtils.param2Obj(config.url);
        console.log("查询专业token：",token)
        majorRecord=majorRecord.filter(item=>item.majorId!==majorId);
        return {
            code:1,
            msg:'删除专业成功'
        }
    },
    //返回搜索到的专业信息
    queryMajor:config=>{
        const { collegeId, majorName, currentPage, rows,token } = mUtils.param2ObjPost(config.body);
        console.log("查询专业token：",token)
        console.log("查询专业，学院Id：",majorName,collegeId);
        const conlist=majorRecord.filter(item=>item.name.includes(majorName));
        const list = conlist.filter((item,index) => index < rows * currentPage && index >= rows * (currentPage - 1))
        return {
            data:{
                majorRecord:list,
                count:conlist.length
            },
            code:1,
            msg:'查找专业成功'
        }
    },
    //新建专业
    addMajor:config=>{
        const imf = mUtils.param2ObjPost(config.body);
        console.log("添加专业中token",imf.token);
        const info={
            majorId:(Math.floor(Math.random()*100)+100).toString(),
            name:imf['majorRecord[name]'],
            collegeName:imf['majorRecord[collegeName]']
        };
        majorRecord.unshift(info);
        return {
            code:1,
            msg:'添加专业成功'
        }
    },
    //编辑表格中某行的的专业信息
    modifyMajor:config=>{
        const imf = mUtils.param2ObjPost(config.body);
        console.log("修改专业中token",imf.token);
        majorRecord.some(item=>{
            if(item.majorId===imf['majorRecord[majorId]']){
                item.name=imf['majorRecord[name]'];
                item.collegeName=imf['majorRecord[collegeName]'];
            }
        });
        return {
            code:1,
            msg:'修改专业成功'
        }
    },
    //批量删除
    batchDeleteMajor:config=>{
        let ids = mUtils.param2ObjPost(config.body);
        console.log("添加专业中token",ids.token);
        const num=[];
        for(let key in ids){
            num.push(ids[key])
        }
        majorRecord=majorRecord.filter(item=>!num.includes(item.majorId))
        return {
            code:1,
            msg:'批量删除专业成功'//对组件中的ManageMajor的
        }
    },
}
